package com.whfc.ms.api.hdc.controller;

import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.fuum.entity.SysUser;
import com.whfc.hdc.dto.AlgDeviceDataDTO;
import com.whfc.hdc.params.AlgDeviceConfigParam;
import com.whfc.ms.api.hdc.service.MsAlgService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 算法盒子设备
 *
 * @author qzexing
 * @version 1.0
 * @date 2021-07-19 16:30
 */
@RequiresPermissions("hdc/alg")
@RestController
@RequestMapping("/ms/api/hdc/alg/device")
public class AlgDeviceController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MsAlgService msAlgService;

    @GetMapping("/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("pageNum") Integer pageNum,
                       @RequestParam("pageSize") Integer pageSize,
                       @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info("获取算法盒子列表, loginer:{}, pageNum:{}, pageSize:{}, keyword:{}"
                , user.getUsername(), pageNum, pageSize, keyword);
        PageData<AlgDeviceDataDTO> pageData = msAlgService.list(pageNum, pageSize, keyword);
        return ResultUtil.success(pageData);
    }

    @GetMapping("/data/log")
    public Result dataLog(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                          @RequestParam("sn") String sn,
                          @RequestParam("pageNum") Integer pageNum,
                          @RequestParam("pageSize") Integer pageSize,
                          @RequestParam("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                          @RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {
        logger.info("获取算法盒子历史数据, loginer:{}, sn:{}, pageNum:{}, pageSize:{}, startTime:{}, endTime:{}",
                user.getUsername(), sn, pageNum, pageSize, startTime, endTime);
        PageData<AlgDeviceDataDTO> pageData = msAlgService.getDataLog(sn, pageNum, pageSize, startTime, endTime);
        return ResultUtil.success(pageData);
    }


    @GetMapping("/config")
    public Result dataLog(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                          @RequestParam("sn") String sn,
                          @RequestParam("configCode") String configCode) {
        logger.info("获取算法盒子配置信息, loginer:{}, sn:{}, configCode:{}", user.getUsername(), sn, configCode);
        List<String> list = msAlgService.getConfig(sn, configCode);
        return ResultUtil.success(new ListData<>(list));
    }

    @PostMapping("/configure")
    public Result dataLog(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                          @RequestBody AlgDeviceConfigParam params) {
        logger.info("设置算法盒子配置, loginer:{}, params:{}", user.getUsername(), params);
        msAlgService.setConfig(params);
        return ResultUtil.success();
    }

}
